require_relative '../test_helper'

module Resources
  class ParamsTest < Resources::Test
    def test_ignore_extra_parameters
      post path('users'),
           { user: { name: 'xiaohong', age: 18, extra: 'extra' } }.to_json,
           { 'CONTENT_TYPE' => 'application/json' }
      assert last_response.created?
      assert_equal({ user: { id: 3, name: 'xiaohong', age: 18 } }.as_json, JSON.parse(last_response.body))
    end

    def test_provide_string_type_for_integer_param
      post path('users'),
           { user: { name: 'xiaohong', age: '18' } }.to_json,
           { 'CONTENT_TYPE' => 'application/json' }
      assert last_response.created?
      assert_equal({ user: { id: 3, name: 'xiaohong', age: 18 } }.as_json, JSON.parse(last_response.body))
    end

    def test_provide_invalid_string_type_for_integer_param
      post path('users'),
           { user: { name: 'xiaohong', age: 'age' } }.to_json,
           { 'CONTENT_TYPE' => 'application/json' }
      assert last_response.bad_request?
      assert_equal 'parameter_invalid', JSON.parse(last_response.body)['code']
    end

    def test_missing_required_param
      post path('users')
      assert last_response.bad_request?
      assert_equal 'parameter_invalid', JSON.parse(last_response.body)['code']
    end
  end
end
